home *** CD-ROM | disk | FTP | other *** search
- /* C O P Y R I G H T N O T I C E : */
- /* Copyright 1986 Eric Jul and Norm Hutchinson. May not be used for any */
- /* purpose without written permission from the authors. */
-
- #include "Kernel/h/system.h"
- #include "Kernel/h/assert.h"
- #include "Kernel/h/expandArray.h"
-
- void CreateArray(fArrayPtr, fSize)
- int **fArrayPtr;
- int fSize;
- /* Create an array of the specified size */
- {
- *fArrayPtr = (int *) malloc((unsigned)fSize);
- bzero((char *) *fArrayPtr, (fSize));
- }
-
- void DiscardArray(fArrayPtr)
- int **fArrayPtr;
- /* Discard the array, freeing up its storage */
- {
- free((char *)*fArrayPtr);
- *fArrayPtr = (int *) 0;
- }
-
- void ExpandArray(fArrayPtr, fOldSize, fNewSize)
- int **fArrayPtr;
- int fOldSize;
- int fNewSize;
- /* Expand the array to fNewSize (>= fOldSize assumed).
- The elements are copied. */
- {
- register int *newArray;
-
- assert(fNewSize >= fOldSize);
- newArray = (int *) malloc((unsigned)fNewSize);
- /* Copy over the old part */
- bcopy((char *)*fArrayPtr, (char *)newArray, fOldSize);
- free((char *)*fArrayPtr);
- /* Zero out the new part of the new array */
- bzero(((char *) newArray) + fOldSize, fNewSize - fOldSize);
- *fArrayPtr = newArray;
- }
-
- void EnsureArray(fArrayPtr, fOldSize, fNewSize)
- int **fArrayPtr;
- int fOldSize;
- int fNewSize;
- /* Ensure that the size of the array is at least fNewSize */
- {
- if (*fArrayPtr) {
- if (fOldSize < fNewSize) ExpandArray(fArrayPtr, fOldSize, fNewSize);
- } else {
- CreateArray(fArrayPtr, fNewSize);
- }
- }
-
- /**********************************************************************/
- /* Dynamic array */
-
- void DynAddUpper(fArr, fElem)
- DynArray fArr;
- int fElem;
- /* Add another element at the upper bound, expand if necessary */
- {
- if (fArr->count == fArr->maxCount) {
- int oldSize = fArr->allocSize;
- fArr->allocSize += fArr->allocSize;
- fArr->maxCount += fArr->maxCount;
- ExpandArray(&fArr->array, oldSize, fArr->allocSize);
- }
- fArr->array[fArr->count++] = fElem;
- }
-
- void DynPut(fArr, fIndex, fElem)
- DynArray fArr;
- int fIndex;
- int fElem;
- /* Put this into an array */
- {
- assert ( 0 <= fIndex && fIndex < fArr->count );
- fArr->array[fIndex] = fElem;
- }
-
- void DynRemove(fArr, fIndex)
- DynArray fArr;
- int fIndex;
- /* Remove an entry, compacting the arrry */
- {
- assert ( 0 <= fIndex && fIndex < fArr->count );
- if (fIndex < --fArr->count) {
- fArr->array[fIndex] = fArr->array[fArr->count];
- }
- }
-
- void DynClear(fArr)
- DynArray fArr;
- /* Reset the array */
- {
- fArr->count = 0;
- }
-
- int DynCount(fArr)
- DynArray fArr;
- /* Return the current count */
- {
- return fArr->count;
- }
-
- int DynGet(fArr, fIndex)
- DynArray fArr;
- int fIndex;
- /* Get the element with the specified index */
- {
-
- return fArr->array[fIndex];
- }
-
- DynArray DynCreate(fInitialCount)
- int fInitialCount;
- /* Create a DynArray with initail room for the specified element count */
- {
- DynArray arr = (DynArray) malloc(sizeof(DynArrayRec));
- arr->allocSize = fInitialCount * sizeof(int);
- arr->count = 0;
- arr->maxCount = fInitialCount;
- CreateArray(&arr->array, arr->allocSize);
- return arr;
- }
-
- /**********************************************************************/
-